package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.ToolsWedBudgetActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.BudgetBean;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.GiftBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.common.MyViewHolder;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.view.SwipteLayout2;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/2
 * @description :
 */
public class ToolsPager_Budget_RecommendList extends BasePager implements View.OnClickListener, OnNetResultListener {
    private ToolsWedBudgetActivity mBudgetActivity;
    private View mContentview;
    private LinearLayout mBackLay;
    private LinearLayout mEmptyLay;
    private TextView mAddBudgetTv;
    private LinearLayout mContentRlay;
    private TextView mCityTv;
    private ImageView mDownArrIv;
    private TextView mMoneyCountTv;
    private ListView mBudgetLview;
    private RelativeLayout mBudgetListHeader;
    private Button mCommitBtn;
    private ProgressBar mProgressbar;
    private SwipteLayout2 mSwipeLay;
    private String mobileNum;
    private List<BudgetBean.DataBean> budgetList;
    private int budgetCount;
    private boolean isEdit;
    private CommonAdapter myAdapter;
    private BudgetBean budgetBean;
    private boolean isDelete;
    private int mCurrentPosition;
    public ToolsPager_Budget_RecommendList(Activity activity) {
        super(activity);
    }


    @Override
    protected View getView() {
        mBudgetActivity = (ToolsWedBudgetActivity) mActivity;
        mContentview = View.inflate(mActivity, R.layout.pager_tools_budget_recommend, null);
        mBackLay = (LinearLayout) mContentview.findViewById(R.id.llay_tools_wedregister_back);
        mEmptyLay = (LinearLayout) mContentview.findViewById(R.id.llay_tools_gift_emptygift);
        mAddBudgetTv = (TextView) mContentview.findViewById(R.id.tv_tools_gift_addgift);
        mContentRlay = (LinearLayout) mContentview.findViewById(R.id.rlay_tools_budgetcontent);
        mCityTv = (TextView) mContentview.findViewById(R.id.tv_tools_wedregistry_place);
        mDownArrIv = (ImageView) mContentview.findViewById(R.id.iv_head_downarr);
        mMoneyCountTv = (TextView) mContentview.findViewById(R.id.tv_tools_moneycount);
        mBudgetLview = (ListView) mContentview.findViewById(R.id.lview_tools_budget);
        mBudgetListHeader = (RelativeLayout) mContentview.findViewById(R.id.rlay_tools_budgithead);
        mCommitBtn = (Button) mContentview.findViewById(R.id.btn_tools_budget_editcommit);
        mProgressbar = (ProgressBar) mContentview.findViewById(R.id.progressBar);
        mCityTv.setText("编辑");
        mDownArrIv.setVisibility(View.GONE);
        mProgressbar.setVisibility(View.VISIBLE);
        mBackLay.setOnClickListener(this);
        mAddBudgetTv.setOnClickListener(this);
        mCityTv.setOnClickListener(this);
        mCommitBtn.setOnClickListener(this);
        mEmptyLay.setVisibility(View.VISIBLE);
        return mContentview;
    }
    @Override
    public void initData() {
        User user = DataWareHourse.getInstance().getmUser();
        mobileNum = user.getData().get(0).getNvc_MobilePhone();
        budgetBean = mBudgetActivity.getmBudgitData();
        budgetCount = mBudgetActivity.getBudgetCount();
        mMoneyCountTv.setText("" + budgetCount);
        if (budgetBean != null) {
            budgetList = budgetBean.getData();
            if (budgetList != null && budgetList.size() > 0) {
                myAdapter = new CommonAdapter<BudgetBean.DataBean>(mBudgetActivity, budgetList, R.layout.item_tools_budget_contentitem) {
                    @Override
                    public void convert(MyViewHolder holder, int position) {
                        BudgetBean.DataBean dataBean = budgetList.get(position);
                        holder.setText(R.id.name, dataBean.getWBItemName());
                        holder.setText(R.id.tv_tools_budgetmoney, "" + dataBean.getWBItemAmount());
                        Button mDelBtn = (Button) holder.getView(R.id.btn_delete);
                        SwipteLayout2 mSwipLay = (SwipteLayout2) holder.getView(R.id.llay_tools_dayplan_mainitem);
                        mSwipLay.setOnItemStateChanged(new SwipteLayout2.OnItemStateChanged() {
                            @Override
                            public void onOpen(SwipteLayout2 layout) {
                                mSwipeLay = layout;
                            }
                            @Override
                            public void onClose(SwipteLayout2 layout) {
                                if (mSwipeLay == layout) {
                                    mSwipeLay = null;
                                }
                            }
                            @Override
                            public void onDown(SwipteLayout2 layout) {
                                if (mSwipeLay != null && mSwipeLay != layout) {
                                    mSwipeLay.closeMenu();
                                }
                            }
                        });
                        mDelBtn.setTag(position);
                        mDelBtn.setOnClickListener(ToolsPager_Budget_RecommendList.this);

                    }
                };
                mBudgetLview.setAdapter(myAdapter);
                mProgressbar.setVisibility(View.GONE);
                mEmptyLay.setVisibility(View.GONE);

            }
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_tools_budget_editcommit://保存
                String url = UrlDealUtil.getRealUrl(RequestCode.SAVE_WBITEM);
                HashMap<String, String> requestParams = new HashMap<String, String>();
                StringBuffer persontsb = new StringBuffer();
                StringBuffer temAmountsb = new StringBuffer();
                StringBuffer temNamesb = new StringBuffer();
                int tableCount = mBudgetActivity.getTableCount();
                if (budgetList.size() > 0) {
                    for (int i = 0; i < budgetList.size(); i++) {
                        BudgetBean.DataBean dataBean = budgetList.get(i);
                        if (i == budgetList.size() - 1) {
                            persontsb.append(dataBean.getPersent() + "");
                            temAmountsb.append(dataBean.getWBItemAmount() + "");
                            temNamesb.append(dataBean.getWBItemName() + "");
                        } else {
                            persontsb.append(dataBean.getPersent() + ",");
                            temAmountsb.append(dataBean.getWBItemAmount() + ",");
                            temNamesb.append(dataBean.getWBItemName() + ",");
                        }
                    }
                    requestParams.put("BudgetTables", "" + tableCount);
                    requestParams.put("Persent", "" + persontsb.toString());
                    requestParams.put("WBItemAmount", "" + temAmountsb.toString());
                    requestParams.put("WBItemName", "" + temNamesb.toString());

                } else {
                    requestParams.put("BudgetTables", "" + tableCount);
                    requestParams.put("Persent", "");
                    requestParams.put("WBItemAmount", "");
                    requestParams.put("WBItemName", "");
                }
                HttpRequestBean budgithttp = new HttpRequestBean(RequestCode.POSTEQUEST, url, this,
                        CommonResultBean.class, RequestCode.SAVE_WBITEM, requestParams);
                HttpHandleUtil.executeCommonPost(mBudgetActivity, budgithttp);
                showCommitPopupWindow(view);
                break;
            case R.id.llay_tools_wedregister_back://返回
                mBudgetActivity.getSupportFragmentManager().popBackStack();
                mBudgetActivity.finish();
                break;

            case R.id.tv_tools_wedregistry_place://编辑
                mBudgetActivity.getSupportFragmentManager().popBackStack();
                mBudgetActivity.setFragment("编辑结婚预算明细页");
                break;
            case R.id.btn_delete://删除item
                isDelete = true;
                mCurrentPosition = (int) view.getTag();
                budgetList.remove(mCurrentPosition);
                if (myAdapter != null) {
                    myAdapter.notifyDataSetChanged();
                }
                if (mSwipeLay != null) {
                    mSwipeLay.closeMenu();
                }
                break;

        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (mCommitpop != null) {
            mCommitpop.dismiss();
        }
        if (data instanceof GiftBean) {
            GiftBean mGiftBean = (GiftBean) data;
            switch (mGiftBean.getResCode()) {
                case "0000":
                    mProgressbar.setVisibility(View.GONE);
                    mEmptyLay.setVisibility(View.GONE);
                    break;
                case "0001":
                case "0002":
                case "0003":
                    mEmptyLay.setVisibility(View.VISIBLE);
                    break;
            }
        } else if (data instanceof CommonResultBean) {
            CommonResultBean commonResult = (CommonResultBean) data;
            switch (commonResult.getResCode()) {
                case "0000":
                    Toast.makeText(mBudgetActivity, "保存成功，可在我的-我的结婚预算中查看预算清单", Toast.LENGTH_LONG).show();
                    break;
                case "0001":
                case "0002":
                case "0003":
                    CUtils.showToast("数据请求失败！");
                    break;
            }
        }
    }
    @Override
    public void onError(ErrorBean errorBean) {
        if (mCommitpop != null) {
            mCommitpop.dismiss();
        }
        CUtils.showToast("数据请求错误！");
    }
    @Override
    public void notifyData() {
        budgetCount = mBudgetActivity.getBudgetCount();
        budgetBean = mBudgetActivity.getmBudgitData();
        mMoneyCountTv.setText("" + budgetCount);
        budgetList = budgetBean.getData();
        if (myAdapter != null) {
            myAdapter.notifyDataSetChanged();
        }
    }
}
